package main

import (
	"fmt"
	"net"
)

func main() {
	sock, err := net.Listen("tcp", "127.0.0.1:8080")
	fmt.Println("欢迎使用白墨聊天系统！")
	if err != nil {
		fmt.Println(err)
	}
	for {
		// 创建监听对象
		conn, err := sock.Accept()
		defer conn.Close()

		if err != nil {
			fmt.Println(err)
		}

		conn.Write([]byte("欢迎使用白墨聊天系统！"))

		for {
			// 接收聊天信息
			rstmsg := make([]byte, 1024)
			n, err := conn.Read(rstmsg)
			if err != nil {
				fmt.Println(err)
				break
			}

			if string(rstmsg[:n]) == "q" {
				break
			}
			fmt.Println(string(rstmsg[:n]))

			// 发送聊天信息
			var sendmsg string
			fmt.Printf("您的发送内容：")
			fmt.Scanln(&sendmsg)
			conn.Write([]byte(sendmsg))
		}
	}

}